package cn.wxq.daily.empdailymgforserve.controller;

import cn.wxq.daily.empdailymgforserve.domain.Vacation;
import cn.wxq.daily.empdailymgforserve.model.Result;
import cn.wxq.daily.empdailymgforserve.model.StatusCode;
import cn.wxq.daily.empdailymgforserve.service.VacationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
@CrossOrigin //跨越处理
@RestController
@RequestMapping("/vacation")
@Api(value = "VacationController||出勤休假控制器")
public class VacationController {
    @Autowired
    private VacationService vacationService;

    /**
     * 测试
     *
     * @param
     * @return
     */
    @RequestMapping(value = "/findAll", method = RequestMethod.POST)
    @ApiOperation(value = "查询测试")
    public Result findAll() {
        List<Vacation> res = vacationService.findALL();
        if (res != null) {
            return new Result(true, StatusCode.OK, "获取成功", res);
        } else {
            return new Result(false, StatusCode.ERROR, "返回数据异常");
        }
    }

    /**
     * 条件查询
     *
     * @param vacation
     * @return
     */
    @RequestMapping(value = "/findByPage", method = RequestMethod.POST)
    @ApiOperation(value = "按条件查询")
    public Result findByPage(Vacation vacation) {
        List<Vacation> res = vacationService.findByPage(vacation);
        if (res != null) {
            return new Result(true, StatusCode.OK, "获取成功", res);
        } else {
            return new Result(false, StatusCode.ERROR, "返回数据异常");
        }
    }

    /**
     * 新增
     *
     * @param vacation
     * @return
     */
    @RequestMapping(value = "/addVacation", method = RequestMethod.POST)
    @ApiOperation(value = "新增")
    public Result addVacation(Vacation vacation) {
        vacationService.insertVacation(vacation);
        return new Result(true, StatusCode.OK, "新增成功");
    }

    /**
     * 修改
     *
     * @param vacation
     * @return
     */
    @RequestMapping(value = "/updateVacation", method = RequestMethod.POST)
    @ApiOperation(value = "修改")
    public Result updateVacation(Vacation vacation) {
        int row = vacationService.updateVacation(vacation);
        if (row >0){

            return new Result(true, StatusCode.OK, "修改成功");
        }else {
            return new Result(false, StatusCode.ERROR, "失败");

        }
    }
    /**
     * 删除
     *
     * @param leaveId
     * @return
     */
    @RequestMapping(value = "/deleteById", method = RequestMethod.POST)
    @ApiOperation(value = "删除")
    public Result deleteById(Integer leaveId) {
        vacationService.deleteById(leaveId);
        return new Result(true, StatusCode.OK, "删除成功");
    }

    /**
     * id查询详情
     *
     * @param leaveId
     * @return
     */
    @RequestMapping(value = "/findById", method = RequestMethod.POST)
    @ApiOperation(value = "id查询详情")
    public Result findById(Integer leaveId) {
        Vacation vacation = vacationService.selectVacationById(leaveId);
        return new Result(true, StatusCode.OK, "成功",vacation);
    }

    @RequestMapping(value = "/findByUserName", method = RequestMethod.POST)
    @ApiOperation(value = "通过用户名字查询")
    public Result findByUserName(String nickName) {
        List<Vacation>  vacation = vacationService.selectVacationByUserName(nickName);
        return new Result(true, StatusCode.OK, "成功",vacation);
    }

}
